<template>
    <div class="unit-bets">
        <div class="u-header">
            <div class="title">{{title}}</div>
            <div class="input">
                批量设置
                <input type="number" @input="batch($event)">
                <div class="hidden" @click="hiddenBody">隐藏</div>
            </div>
        </div>
        <div class="u-body" :style="{display: hidden ? 'none' : 'flex'}">
            <div
                    class="item"
                    v-for="item of oddsFields">
                <span class="number">{{item.field}}</span>
                <input class="odds" type="text" ref="input" :value="item.odds" @input="doThis"/>
            </div>
            <div class="item null" v-for="item of nullValue"></div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "unit",
        data () {
            return {
                nullValue: this.fields.length % 3,
                hidden: false,
            }
        },
        props: ['title', 'fields', 'defaultValude', 'odds'],
        methods: {
            doThis() {
                let odds = []
                this.$refs.input.forEach((v, k) => {
                    odds.push({
                        field: this.fields[k],
                        odds: v.value
                    })
                })
                this.$emit('input', odds)
            },
            batch (e) {
                this.$refs.input.forEach(v => {
                    v.value = e.target.value
                })
                this.doThis()
            },
            hiddenBody () {
                this.$set(this, 'hidden', ! this.hidden)
            }
        },
        computed: {
            oddsFields() {
                let odds = []
                this.fields.forEach((v, k) => {
                    if (this.odds[k]) {
                        odds.push(this.odds[k])
                    } else {
                        odds.push({
                            field: v,
                            odds: this.defaultValude
                        })
                    }
                })
                return odds
            }
        },
        watch: {
            batch (val) {
                let odds = {}
                this.f = val
                this.emit(this.oddsInput)
            }
        }
    }
</script>

<style scoped lang="less">
@import "../../assets/css/bases";
    .unit-bets {
        .u-header {
            height: 35px;
            background: @gray;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .title {
                width: 120px;
                text-indent: 10px;
                font-size: 1.1rem;
            }
            .input {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                margin-right: 10px;
                input {
                    border: none;
                    border-radius: 5px;
                    background: #fff799;
                    width: 60px;
                    height: 20px;
                    outline: none;
                    text-align: center;
                    margin-left: 5px;
                }
            }
            .bets, .hidden {
                margin-left: 10px;
                text-decoration:underline
            }
        }
        .u-body {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            padding: 0 10px;
            .item {
                margin-top: 10px;
                border: @gray solid 1px;
                border-radius: 5px;
                width: 28%;
                padding: 5px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                color: @error;
                .number {
                    width: 20px;
                    height: 20px;
                    line-height: 20px;
                    text-align: center;
                    font-size: @default;
                }
                span {
                    color: white;
                    background: @error
                }
                .odds {
                    background: white;
                    width: 50px;
                    border: none;
                    height: 20px;
                    text-align: right;
                    outline: none;
                }
            }
            .item.btn {
                background: @wran;
                color: white;
                border: @wran solid 1px;
                justify-content: center;
            }
            .item.null {
                border: none;
            }
            .item.chiose {
                border: #ccc solid 1px;
                background: #ccc;
            }
        }
        .u-body.hidden {
            display: none;
        }
    }
</style>